(function($) {

	$(document).ready(function() 
	{


			if ($("#main-tab").length > 0 )
			{
				$( "#main-tab" ).tabs({
					  	active   : $.cookie('activetab'),
					    activate : function( event, ui ){
					        $.cookie( 'activetab', ui.newTab.index(),{
					            expires : 10
					        });
					    }
				});
			}

			$(".color-picker").wpColorPicker();

			if ($(".wo-wrap-subtabs").length > 0 )
			{
				$( ".wo-wrap-subtabs" ).tabs({
					  	active   : $.cookie('subtabs'),
					    activate : function( event, ui ){
					        $.cookie( 'subtabs', ui.newTab.index(),{
					            expires : 10
					        });
					    }
				});
			}

			if ($(".inner-tab").length > 0 )
			{
				$( ".inner-tab" ).tabs({
					active   : $.cookie('activeinnertab'),
				    activate : function( event, ui ){
				        $.cookie( 'activeinnertab', ui.newTab.index(),{
				            expires : 10
				        });
				    }
				});
			}
			
			if ($("#accordion").length > 0 )
			{
				$( "#accordion" ).accordion();
			}


			
			

			$(".wo-logo-type").change(function()
			{	
				var logoType = $(this).val();
				
				if (logoType == 'text')
				{
					$(".upload-logo").fadeOut();
					// $(".image_fixed").fadeOut();
					$(".logo-text").fadeIn();
				}else{
					$(".logo-text").fadeOut();
					// $(".image_fixed").fadeOut();
					$(".upload-logo").fadeIn();
				}
			}).trigger("change");

		

			var formSocial1 =  '<div class="form-group item form-social fix-form wrap-social wo-footer-socials">\
                                              <a class="icon thickbox available-panel"href="#TB_inline?width=600&height=550&inlineId=table-fa"><i class="fa fa-refresh"></i></a>\
                                              <input type="hidden" name="theme_options[footer][social][social_icon][]" value="fa fa-refresh">\
                                              <input type="text" name="theme_options[footer][social][social_link][]" value="" placeholder="Link">\
                                              <a class="remove-socials" data-class="remove"><i class="fa fa-times"></i></a>\
                                   </div>';

			$(".wo-footer-addsocial").on("click", function(event)
			{
				event.preventDefault();
				

                $(this).parents(".wo-add-social").before(formSocial1);                   
			})

			$(".remove-socials").live("click", function(event)
			{
				event.preventDefault();
				$(this).parents(".wo-footer-socials").fadeOut("slow", function(){$(this).remove()});

	
				if ( ($(".wo-footer-socials").length == 1) )
				{
					$(".wo-add-social").before(formSocial1);         
				}
			})

			$(document).on("click", ".upload-img",function()
			{

				// var options = {
				// 		frame:    'post',
				// 		state:    'insert',
				// 		title:    wp.media.view.l10n.addMedia,
				// 		multiple: true
				// 	};		
				
				// wp.media.editor.open( "content", options );

	            var current = $(this), insertTo = current.data("append"), insertLink=current.data("insertLink");
	           
	            var send_attachment_bkp = wp.media.editor.send.attachment;
	            wp.media.editor.send.attachment = function(props, attachment) {

	                var image_size =  props.size;
	                var image_url = "";

	                if ( typeof attachment.sizes !== 'undefined' )
	                {
	                	image_url = attachment.sizes[image_size].url
	                }else{
	                	image_url = attachment.url;
	                }

	                var insert_link = "";
	                
	                if ( current.hasClass("blog-img") )
	                {
	                	insert_link = attachment.id;
	                }else{
	                	insert_link = attachment.url;
	                }


	                current.siblings(insertTo).html('<span><img src="'+image_url+'"></span>');
	                current.siblings(insertLink).val(insert_link);
	                wp.media.editor.send.attachment = send_attachment_bkp;
	            }
	            wp.media.editor.open();
	            return false;
	        });


		  	$(document).on("click", ".qva-design li", function(event)
	        {
	        	event.preventDefault();
	        	var keyData = $(this).data("key");
	        	
	        	$(this).parent().find(".skin-active").removeClass("skin-active");
	        	$(this).parent().next().attr("value", keyData);
	        	$(this).addClass("skin-active");
	        	
	        })
		
			// $(document).on("click", "#save-to-file", function(event)
			// {
			// 	event.preventDefault();
			// 	$.ajax(
			// 	{
			// 		type: 'POST',
			// 		url : QVA_AJAX.ajaxurl,
			// 		action: 'export-data',
			// 		success: function(res)
			// 		{

			// 		}
			// 	})
			// })
			$.download = function(url, data, method){
				if( url && data ){
					data = typeof data == 'string' ? data : jQuery.param(data);
					var inputs = '';
					jQuery.each(data.split('&'), function(){
						var pair = this.split('=');
						inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />';
					});
					jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
						.appendTo('body').submit().remove();
				};
			};
		
			$("input[name=export-theme-options]").click(function(){
				$.download(ajaxurl,'action=export_theme_options_data');
				return false;
			});

			$("input[name=import-theme-options]").click(function(event)
			{
				event.preventDefault();
				var _dataImport = $("textarea[name=data-import]").val(),  _self=$(this), wp_nonce = $("input[name=_wo_nonce]").val(), wp_http_referer = $("input[name=_wp_http_referer]").val();
				
				if (_dataImport == '')
				{
					alert("Please filler your data");
				}else{
					$.ajax(
					{
						url: QVA_AJAX.ajaxurl,
						type: 'POST',
						data: {action: 'import-theme-options', data_import: _dataImport, _wp_nonce: wp_nonce, _wp_http_referer: wp_http_referer },
						beforeSend: function()
						{
							_self.val("Importing...");
							_self.prop("disable", true);
						},
						success: function(res)
						{
							var _parse = $.parseJSON(res);
							// alert(_parse.mes);
							// _self.val("Import");
							if (!_parse.error)
							{
								$("#qva-success").fadeIn();
								_self.val("");
							}else{
								$("#qva-empty-data").fadeIn();
							}
							setTimeout(function(){
								$(".alert").fadeOut('slow');
								_self.prop("disable", false);
							}, 2000);
							_self.val("Import");
						}
					})
				}
			})
	
		 // 	function stripslashes(str) 
		 // 	{
			//   //       discuss at: http://phpjs.org/functions/stripslashes/
			//   //      original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
			//   //      improved by: Ates Goral (http://magnetiq.com)
			//   //      improved by: marrtins
			//   //      improved by: rezna
			//   //         fixed by: Mick@el
			//   //      bugfixed by: Onno Marsman
			//   //      bugfixed by: Brett Zamir (http://brett-zamir.me)
			//   //         input by: Rick Waldron
			//   //         input by: Brant Messenger (http://www.brantmessenger.com/)
			//   // reimplemented by: Brett Zamir (http://brett-zamir.me)
			//   //        example 1: stripslashes('Kevin\'s code');
			//   //        returns 1: "Kevin's code"
			//   //        example 2: stripslashes('Kevin\\\'s code');
			//   //        returns 2: "Kevin\'s code"

			//   return (str + '')
			//     .replace(/\\(.?)/g, function(s, n1) {
			//       switch (n1) {
			//         case '\\':
			//           return '\\';
			//         case '0':
			//           return '\u0000';
			//         case '':
			//           return '';
			//         default:
			//           return n1;
			//       }
			//     });
			// }


			// if ($(".slider").length)
			// {
			// 	$(".slider").switchButton(
			// 	{
			 //     	 	width: 60,
			 //          	height: 30,
			 //          	button_width: 30,
			 //          	checked: true,
			 //          	on_callback: function()
			 //          	{
			 //          		var _parents = $(this);
			 //          		if ( (_parents).hasClass("panel-body") )
			 //          		{
			 //          			(_parents.nextAll()).fadeOut();
			 //          		}
			 //          	}
			 //        });	
			 //    }

		 	$(".toggle-settings").change(function()
		 	{
		 		if ($(this).is(":checked"))
		 		{
		 			($(this).parents(".wo-flag").nextAll()).fadeIn();
		 		}else{
		 			($(this).parents(".wo-flag").nextAll()).fadeOut();
		 		}
		 	}).trigger('change');


		 	// order section
		 	$(".section-order-reset").on("click",function(){
	           $("#sortable input").val("ourservices, aboutus, ourteam, ourwork, ourclients, contact");
	           var order = ['ourservices', 'aboutus', 'ourteam', 'ourwork', 'ourclients', 'contact'];
	           for(var i=0;i<order.length;i++)
	           {
	               $("li[data-name='"+order[i]+"']").detach().appendTo("#sortable");
	           }
	           return false;
	        });

		 	if ($("#sortable").length>0)
		 	{
		 		$("#sortable").sortable(
		 		{
		 			update: function(event, ui) {
	                    var order = [];
	                    $('#sortable li').each( function(e) {
	                        order.push( $(this).attr('data-name'));
	                    });
	                    // join the array as single variable...
	                    var positions = order.join(',');
	                    $("input.section-order").val( positions );
	                    return true;
	                }
		 		})

		 		$( "#sortable" ).disableSelection();
		 	}

		 	$(".select_header_type").change(function()
			{	
				var headerType = $(this).val();
				
				if (headerType == 'slider')
				{
					$(".header_youtube, .image-fixed, .header-settings").fadeOut();
					// $(".image_fixed").fadeOut();
					$(".header_slider").fadeIn();
				}else if(headerType=='video'){
					$(".header_youtube, .header-settings").fadeIn();
					$(".header_slider, .image-fixed").fadeOut();
				}else{
					$(".image-fixed, .header-settings").fadeIn();
					$(".header_slider, .header_youtube").fadeOut();
				}
			}).trigger("change");	

	})
	
	

})(jQuery);	